package com.disc.util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;

/**
 * Java 8 Nashorn js 引擎
 * By Tonight
 */
public class JsUtil {
    private static Invocable invocable;
    static
    {
        try
        {
            ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
            Resource resource = new ClassPathResource("disc/Logid.js");
            engine.eval(new FileReader(resource.getFile()));
            invocable = (Invocable) engine;
        }
        catch (Exception e)
        {

        }
    }

    /**
     * 获取Login
     * @param baiduId
     * @return
     */
    public static String getLogid(String baiduId)
    {
        try
        {
            return (invocable.invokeFunction("getLogid",baiduId)).toString();
        }
        catch (Exception e)
        {

        }
        return "";
    }


    /**
     * 获取sign
     * @param sign1
     * @param sign3
     * @return
     */
    public static String getSign(String sign1,String sign3)
    {
        try
        {
            return (invocable.invokeFunction("getSign",sign3,sign1)).toString();
        }
        catch (Exception e)
        {

        }
        return "";
    }

}
